//
//  FMDBDishManager.m
//  takeout
//
//  Created by Mac on 12-4-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FMDBDishManager.h"
#import "FMDatabase.h"
#import "FMDBManager.h"
#import "DishCategory.h"
#import "Dish.h"
@implementation FMDBDishManager

//插入本地菜系
-(void) insertDishCategories:(FMDatabase *)db dishCategories:(DishCategory *)dishCategory supplierid:(NSInteger)supplierid{
    NSLog(@"supplierid%d",supplierid);
    [db executeUpdate:@"INSERT INTO dish_category(id, name, supplierid) values(?,?,?)",[NSNumber numberWithInt:[dishCategory dishCategoryId]],[dishCategory dishCategoryName],[NSNumber numberWithInt:supplierid]];
    NSLog(@"插入本地菜系");
}


//插入本地菜品
-(void) insertDishes:(FMDatabase *)db dishCategories:(Dish *)dish{
    [db executeUpdate:@"INSERT INTO dish(id, name, picture, price, desc, dish_cate_id) values(?,?,?,?,?,?)",[NSNumber numberWithInt:[dish dishId]],[dish dishName],[dish imgUrl],[NSNumber numberWithFloat:[dish price]],[dish desc],[NSNumber numberWithInt:[dish dishCategoryId]]];
    NSLog(@"插入本地菜品");
}

//查询本地菜系
-(void) selectDishCategory{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM dish_category"];
    while ([rs next]){
        NSLog(@"查询本地菜系id%@ name%@ %@",[rs stringForColumn:@"id"],[rs stringForColumn:@"name"],[rs stringForColumn:@"supplierid"]);
    }
    [rs close];
    [fmdbManager closeDataBase];
    [fmdbManager release];
}

//查询本地菜品
-(void) selectDish{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM dish"];
    while ([rs next]){
        NSLog(@"查询本地菜品id%@ name%@ %@ %@ %@ %@",[rs stringForColumn:@"id"],[rs stringForColumn:@"name"],[rs stringForColumn:@"picture"],[rs stringForColumn:@"price"],[rs stringForColumn:@"desc"],[rs stringForColumn:@"dish_cate_id"]);
    }
    [rs close];
    [fmdbManager closeDataBase];
    [fmdbManager release];
}

//删除菜品
-(void) deleteDish:(NSString *)dishId{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    NSLog(@"删除菜品");
    [db executeUpdate:@"DELETE FROM dish where id=?",dishId];
    [fmdbManager closeDataBase];
    [fmdbManager release];
}

//删除菜系
-(void) deleteDishCategory:(NSString *)DishCategoryId{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    NSLog(@"删除菜系");
    [db executeUpdate:@"DELETE FROM dish_category where id=?",DishCategoryId];
    [fmdbManager closeDataBase];
    [fmdbManager release];
}


//从本地取得菜品
-(NSArray *)getAllDishesBySupIdFromLoc:(NSString *)supplierId{
    NSMutableArray *dishCategorys=[NSMutableArray arrayWithCapacity:2];
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *dishCategoryRs = [db executeQuery:@"select * from dish_category where supplierid=?",supplierId];
    while ([dishCategoryRs next]) {
        DishCategory *dishCategory=[[DishCategory alloc]init];
        dishCategory.dishCategoryId=[dishCategoryRs intForColumn:@"id"];
        dishCategory.dishCategoryName=[dishCategoryRs stringForColumn:@"name"];
        NSString *dishCategoryIdToString = [NSString stringWithFormat:@"%d",dishCategory.dishCategoryId];
        FMResultSet *dishRs=[db executeQuery:@"select * from dish where dish_cate_id=?",dishCategoryIdToString];
        NSMutableArray *dishes=[NSMutableArray arrayWithCapacity:2];
        while([dishRs next]){
            Dish *dish=[[Dish alloc]init];
            dish.dishId=[dishRs intForColumn:@"id"];
            dish.dishName=[dishRs stringForColumn:@"name"];
            dish.imgUrl=[dishRs stringForColumn:@"picture"];
            dish.price=[dishRs doubleForColumn:@"price"];
            dish.desc=[dishRs stringForColumn:@"desc"];
            dish.dishCategoryId=[dishRs intForColumn:@"dish_cate_id"];
            [dishes addObject:dish];
            [dish release];
        }
        dishCategory.dishes=dishes;
        [dishCategorys addObject:dishCategory];
        [dishCategory release];
    }
    [fmdbManager release];
    return dishCategorys;
}




-(void)insertAllDishesBySupIdToLoc:(NSArray *)dishCategories supplierid:(NSInteger)supplierid{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    //    [db executeUpdate:@"DELETE FROM dish"];
    //    [db executeUpdate:@"delete from dish_category"];
    NSString *supplieridToString = [NSString stringWithFormat:@"%d",supplierid];
    NSArray * localDishes=[self getAllDishesBySupIdFromLoc:supplieridToString];
    
    if([localDishes count]==0){
        for(int i=0;i<[dishCategories count];i++){
            DishCategory *dishCategory=(DishCategory *)[dishCategories objectAtIndex:i];
            [self insertDishCategories:db dishCategories:dishCategory supplierid:supplierid];
            for(int i=0;i<[dishCategory.dishes count];i++){
                Dish *dish=(Dish *)[dishCategory.dishes objectAtIndex:i];
                [self insertDishes:db dishCategories:dish];
            }
        }
    } 
    else{
        for(int i=0;i<[dishCategories count];i++){
            DishCategory *dishCategory=(DishCategory *)[dishCategories objectAtIndex:i];
            NSString *dishCategoryIdToString = [NSString stringWithFormat:@"%d",[dishCategory dishCategoryId]];
            [self deleteDishCategory:dishCategoryIdToString];
            [self insertDishCategories:db dishCategories:dishCategory supplierid:supplierid];
            for(int i=0;i<[dishCategory.dishes count];i++){
                Dish *dish=(Dish *)[dishCategory.dishes objectAtIndex:i];
                NSString *dishIdToString = [NSString stringWithFormat:@"%d",[dish dishId]];
                [self deleteDish:dishIdToString];
                [self insertDishes:db dishCategories:dish];
            }
        }
        
    }
    [fmdbManager closeDataBase];
    [fmdbManager release];
}

@end
